package com.apes.fn.scm.used.server;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.fn.scm.used.model.Used;
import com.apes.fn.scm.used.repository.UsedRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.List;
import java.util.Map;

@Service("usedService")
public class UsedService extends DomainService {

    @Autowired
    UsedRepository usedRepository;

    /***
     * 创建
     */
    public Used create(SimpleRequest request) {

        isUniqueName(request);

        Used used = request.getO(Used.class);
        return usedRepository.saveAndFlush(used);
    }

    /***
     * 修改
     */
    public Used update(SimpleRequest request) {
        isUniqueName(request);
        isValid(request, usedRepository);
        Used used = request.getO(Used.class);
        return usedRepository.saveAndFlush(used);
    }


    /***
     * 删除
     */
    public String delete(SimpleRequest request) {
        isValid(request, usedRepository);
        Used used = request.getO(Used.class);
        usedRepository.delete(used);
        return "删除成功！";
    }


    /***
     * 数据是否合法
     * @param request
     * @param repository
     */
    private Model isValid(SimpleRequest request, CustomRepository repository) {
        Model oldModel = (Model) repository.findOne((String) request.get("id"));
        if (oldModel == null) throw new RuntimeException("编码【" + request.get("id") + "】在系统中不存在。");
        if (oldModel.getVersion() != (int) request.get("version")) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        return oldModel;
    }

//    /***
//     * findAllByClassifyId
//     */
//    public JSONObject convertQueryParams(SimpleRequest request) {
//        JSONObject m = request.getJO();
////        "{"condition":[],"domain":{"filters":[{"field":"classify.id","operator":"EQ","value":null}],"groups":[],"operator":"and"},"size":10,"page":0}"
//        if(  )
//
//    }



    /***
     * 验证名称唯一
     */
    private void isUniqueName(SimpleRequest request) {

        Used repeatUsed = request.get("id") == null ?
                usedRepository.findFirstByName(request.get("name")) :
                usedRepository.findFirstByNameAndIdIsNot(request.get("name"), request.get("id"));

        if(repeatUsed != null) {
            throw new RuntimeException("相同名称的旧件已存在，旧件编号:" + repeatUsed.getName());
        }
    }

    /**
     * 旧件有效字段为TRUE就查询有效的旧件，否则查全部
     * @param request
     * @return
     */
    public Page<Used> findByValid(SimpleRequest request) {
        List<Map> filters = request.get("condition");
        for (int i = 0; i < filters.size(); i++) {
            Map map = filters.get(i);
            if ("valid".equals(map.get("field")) && map.get("value").equals(0)) filters.remove(i);
        }
        Page<Used> useds = usedRepository.findAll(JpaDsl.toCriteria(filters), PageRequest.of(request.get("page"), request.get("size")));
        return useds;
    }
}
